<?php 
class HiringModel extends BaseModel {
	public function add_hiring_template($data){
		$data['cid'] = $_SESSION['company']['cid'];
		$data['age'] = $data['age_start'].','.$data['age_end'];
		$data['updateline'] = time();
		$template_id = D('hiring_template')->add($data);
		return $template_id;
	}
	public function get_hiring_template($tid = NULL){
		$cid = $_SESSION['company']['cid'];
		if($tid ==NULL){
			$templates = D('hiring_template')->where('is_del = 0 AND cid = '.$cid)->select();
		}else{
			$templates = D('hiring_template')->where('is_del = 0 AND template_id = '.$tid)->select();
		}
		
		foreach($templates as $k => &$v){
			$age = explode(',',$v['age']);
			$v['age_start'] = $age[0];
			$v['age_end'] = $age[1];
		}
		return $templates;
	}
	public function add_hiring($data){
		$data['brand'] = implode(',',$data['brand']);
		$data['workplace'] = implode(',',$data['workplace']);
		$data['hid'] = $data['hangye'];
		$data['pid'] = $data['position'];
		$data['cid'] = $_SESSION['company']['cid'];
		$data['useip'] = get_client_ip();
		$data['status'] = 0;
		$data['is_del'] = 0;
		$data['ctime'] = time();
		$data['etime'] = time();
		$data['updateline'] = time();
		$hiring_id = D('hiring')->add($data);
		return $hiring_id;
	}
	public function add_hiring_info($data){
		$data['updateline'] = time();
		$data['age'] = $data['age_start'].','.$data['age_end'];
		$result = D('hiring_info')->add($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function save_hiring($data,$hiring_id){
		$data['brand'] = implode(',',$data['brand']);
		$data['workplace'] = implode(',',$data['workplace']);
		$data['hid'] = $data['hangye'];
		$data['pid'] = $data['position'];
		$data['useip'] = '127.0.0.1';
		$data['status'] = 0;
		//$data['updateline'] = time();
		$hiring_id = D('hiring')->where('hiring_id = '.$hiring_id)->save($data);
		return $hiring_id;
	}
	public function save_hiring_info($data,$hiring_id){
		//$data['updateline'] = time();
		$data['age'] = $data['age_start'].','.$data['age_end'];
		$result = D('hiring_info')->where('hiring_id = '.$hiring_id)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function get_hiring_list($hid=NULL){
		$cid = $_SESSION['company']['cid'];
		if($hid == NULL){
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.cid = '.$cid)
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->findPage(20);
		}else{
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring_info.hiring_id = '.$hid)
						->find();
			$age = explode(',',$hirings['age']);
			$hirings['age_start'] = $age[0];
			$hirings['age_end'] = $age[1];
		}
		foreach($hirings['data'] as $k => &$v){
			$t = strtotime("-1 week");
			if($v['ontop'] == 1){
				if($v['ontop_time']<$t){
					D('hiring')->where('hiring_id = '.$v['hiring_id'])->save(array('ontop'=>0));
				}
			}
		//预留计算简历数量位置
			$v['cou'] = D('send_resume')->where('hiring_id ='.$v['hiring_id'].'AND is_del = 0')->count();
		}
		return $hirings;
	}
		

}
?>